import { myGetRequest, myPostRequest } from "../../utils/myRequest.js"
const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    list: [],
    load: true,
    cardCur: 0,
    userInfo:{},
    rankList:[],
    myRank:{}
   
  },
  onLoad() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    var that = this;
    console.log("页面初始化");
    var userInfo;
    try {
       userInfo = wx.getStorageSync('userInfo');
      if (!userInfo) {
        wx.redirectTo({
          url: '../login/login'
        })
      }
    } catch (e) {
      // Do something when catch error
      console.log("页面初始化失败： "+e)
    }
    that.setData({
      userInfo : userInfo
    })
    let list = [{}];
    for (let i = 0; i < 26; i++) {
      list[i] = {};
      list[i].name = String.fromCharCode(65 + i);
      list[i].id = i;
    }
    this.setData({
      list: list,
      listCur: list[0]
    })
  },
  onReady() {
    wx.hideLoading()
    this.getRank()
  },
  //----------------------------------------------------业务----------------------------------------------
  getRank: async function() {
    var that=this
    var userId=that.data.userInfo.id
    var res_total_rank = await myGetRequest({ url: "/user/getTotalRank" });
    var res_my_rank= await myGetRequest({ url:"/user/getMyRank/?userId=" + userId})
    if (res_total_rank.statusCode == 200 && res_my_rank.statusCode==200) {
      if (res_total_rank.data.code == 0 && res_my_rank.data.code == 0) {
        that.setData({
          rankList:res_total_rank.data.data,
          myRank:res_my_rank.data.data
        })
      }
      else {
        wx.showToast({
          title: res_total_rank.data.message,
          icon: 'none',
          duration: 2500

        })
      }
    } else {
      wx.showToast({
        title: '网络错误！',
        icon: 'error',
        duration: 1500
      })
      return;
    }
  },
//------------------------------------------------------界面逻辑设置-----------------------
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  }
})